#include "main.h"   


uint32_t weight_zheng;
uint8_t weight_xiao;
uint32_t maopi;

uint32_t hx711;
uint16_t a;
//按克重为430个数为准
#define weight_canshu 86

KFP KFP_height={0.02,0,0,0,0.001,0.543};
//static uint16_t deposit[10];

void hx711_init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	//SDA
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
 	GPIO_Init(GPIOB, &GPIO_InitStructure);
	//SCL
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
 	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOB,GPIO_Pin_14);
}

uint32_t Read_Count(void)
{

	uint32_t Count = 0;
	uint8_t i;
	SDA = 1;
	Delay_us(1);
	SCL = 0;
	Count = 0;
	while(SDA);
	for(i=0;i<24;i++)
	{
		SCL = 1;
		Count = Count << 1;
		Delay_us(1);
		SCL = 0;
		if(SDA)Count++;
		Delay_us(1);
	}
	SCL=1;
	Count=Count^0x800000;
	Delay_us(1);
	SCL=0;
	return Count;
}

void maopi_get()
{
	maopi = Read_Count();
}

void weight_get()
{
//	//观察窗口
//	OLED_ShowNum(1,1,maopi_zheng,9);
//	static uint8_t i = 0 ;
	hx711 = Read_Count();
	if(hx711 >= maopi)
	{
		//观察窗口
//		OLED_ShowNum(3,1,hx711,9);
		hx711 =  hx711 - maopi;
		
		hx711 = ((u32)((float)hx711/weight_canshu));
		
		if(((hx711-KFP_height.out)>3)||((hx711-KFP_height.out)<(-3))){KFP_height.out =hx711;}//跳变快速赋值
		hx711 = (uint32_t)(kalmanFilter(&KFP_height, (float)hx711));//一阶滤波+++++
		
		weight_zheng = hx711;
		weight_xiao = weight_zheng*2%10;
		weight_zheng=weight_zheng*2/10;
	}
}



